﻿Imports Control_Vuelos_BE
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb

Public Class M_LugarADO
    Dim MiConexion As New ConexionADO
    Dim cmd As New SqlCommand

    Public Function ListarM_Lugar(ByVal p_CodLugar As String) As DataTable
        Try
            Dim cn As New SqlConnection(MiConexion.GetCnx)
            Using cn
                Dim cmd As New SqlCommand("sp_Lista_M_Lugar", cn)
                Using cmd
                    cmd.CommandType = Data.CommandType.StoredProcedure
                    cmd.Parameters.Add("@p_CodLugar", SqlDbType.Char, 3)
                    cmd.Parameters("@p_CodLugar").Value = IIf(p_CodLugar Is String.Empty, Nothing, p_CodLugar.Trim)
                    Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
                    Dim dt As DataTable = New DataTable("Lugar")
                    da.Fill(dt)
                    Return dt
                End Using
            End Using
        Catch myException As Exception
            MsgBox("M_LugarADO (ListarM_Lugar) --> " & myException.Message, MsgBoxStyle.Critical)
            Return Nothing
        End Try
    End Function

    Public Function pr_AgregarM_Lugar(ByVal p_M_LugarBE As M_LugarBE) As Boolean
        Dim cn As New SqlConnection(MiConexion.GetCnx)
        Using cn
            cn.Open()
            Dim oTran As SqlTransaction = cn.BeginTransaction(IsolationLevel.Serializable)
            Dim sInstruccion As String = "INSERT INTO dbo.M_Lugar(CodLugar,NomLugar) VALUES ( " & _
                "@CodLugar,@NomLugar) "
            Dim cmd As New SqlCommand(sInstruccion, cn)
            cmd.Transaction = oTran
            cmd.Parameters.AddWithValue("@CodLugar", p_M_LugarBE.CodLugar)
            cmd.Parameters.AddWithValue("@NomLugar", p_M_LugarBE.NomLugar)
            Try
                cmd.ExecuteNonQuery()
                oTran.Commit()
                Return True
            Catch myException As Exception
                MsgBox("M_LugarADO (pr_AgregarM_Lugar) --> " & myException.Message, MsgBoxStyle.Critical)
                oTran.Rollback()
                Return False
            End Try
        End Using
    End Function

    Public Function pr_ModificarM_Lugar(ByVal p_M_LugarBE As M_LugarBE) As Boolean
        Dim cn As New SqlConnection(MiConexion.GetCnx)
        Using cn
            cn.Open()
            Dim oTran As SqlTransaction = cn.BeginTransaction(IsolationLevel.Serializable)
            Dim sInstruccion As String = "UPDATE dbo.M_Lugar  SET NomLugar= @NomLugar WHERE " & _
                " CodLugar  = @CodLugar "
            Dim cmd As New SqlCommand(sInstruccion, cn)
            cmd.Transaction = oTran
            cmd.Parameters.AddWithValue("@CodLugar", p_M_LugarBE.CodLugar)
            cmd.Parameters.AddWithValue("@NomLugar", p_M_LugarBE.NomLugar)
            Try
                cmd.ExecuteNonQuery()
                oTran.Commit()
                Return True
            Catch myException As Exception
                MsgBox("M_LugarADO (pr_ModificarM_Lugar) --> " & myException.Message, MsgBoxStyle.Critical)
                oTran.Rollback()
                Return False
            End Try
        End Using
    End Function


    Public Function pr_EliminarM_Lugar(ByVal p_M_LugarBE As M_LugarBE) As Boolean
        Dim cn As New SqlConnection(MiConexion.GetCnx)
        Using cn
            cn.Open()
            Dim oTran As SqlTransaction = cn.BeginTransaction(IsolationLevel.Serializable)
            Dim sInstruccion As String = "DELETE FROM dbo.M_Lugar  WHERE " & _
                " CodLugar  = @CodLugar "
            Dim cmd As New SqlCommand(sInstruccion, cn)
            cmd.Transaction = oTran
            cmd.Parameters.AddWithValue("@CodLugar", p_M_LugarBE.CodLugar)
            Try
                cmd.ExecuteNonQuery()
                oTran.Commit()
                Return True
            Catch myException As Exception
                MsgBox("M_LugarADO (pr_EliminarM_Lugar) --> " & myException.Message, MsgBoxStyle.Critical)
                oTran.Rollback()
                Return False
            End Try
        End Using
    End Function

End Class
